<template>
  <div class="register-container">
    <div class="register-box">
      <div class="title">
        <h2>导师账号注册</h2>
      </div>
      <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form" label-width="80px">
        <el-form-item label="工号" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入工号"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="registerForm.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码"></el-input>
        </el-form-item>
        <el-form-item label="学院" prop="college">
          <el-input v-model="registerForm.college" placeholder="请输入所属学院"></el-input>
        </el-form-item>
        <el-form-item label="系部" prop="department">
          <el-input v-model="registerForm.department" placeholder="请输入所属系部"></el-input>
        </el-form-item>
        <el-form-item label="职称" prop="title">
          <el-select v-model="registerForm.title" placeholder="请选择职称" style="width: 100%">
            <el-option label="教授" value="教授"></el-option>
            <el-option label="副教授" value="副教授"></el-option>
            <el-option label="讲师" value="讲师"></el-option>
            <el-option label="助教" value="助教"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="registerForm.gender">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="registerForm.phone" placeholder="请输入电话号码"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerForm.email" placeholder="请输入邮箱地址"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="handleRegister" class="register-button">注册</el-button>
          <el-button @click="goToLogin" class="back-button">返回登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'Register',
  data() {
    // 确认密码验证规则
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    
    // 手机号验证规则
    const validatePhone = (rule, value, callback) => {
      if (value && !/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error('请输入有效的手机号码'))
      } else {
        callback()
      }
    }
    
    // 邮箱验证规则
    const validateEmail = (rule, value, callback) => {
      if (value && !/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(value)) {
        callback(new Error('请输入有效的邮箱地址'))
      } else {
        callback()
      }
    }
    
    return {
      registerForm: {
        username: '',
        name: '',
        password: '',
        confirmPassword: '',
        role: 'TEACHER', // 默认为导师角色
        college: '',
        department: '',
        title: '',
        gender: '男',
        phone: '',
        email: ''
      },
      registerRules: {
        username: [
          { required: true, message: '请输入工号', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ],
        college: [
          { required: true, message: '请输入所属学院', trigger: 'blur' }
        ],
        department: [
          { required: true, message: '请输入所属系部', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请选择职称', trigger: 'change' }
        ],
        phone: [
          { validator: validatePhone, trigger: 'blur' }
        ],
        email: [
          { validator: validateEmail, trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          
          // 准备提交的数据
          const userData = {
            username: this.registerForm.username,
            password: this.registerForm.password,
            name: this.registerForm.name,
            role: this.registerForm.role,
            gender: this.registerForm.gender,
            phone: this.registerForm.phone,
            email: this.registerForm.email
          }
          
          // 直接注册用户
          request.post('/auth/register', userData)
            .then(res => {
              if (res.code === 200) {
                // 用户注册成功后，添加导师详细信息
                const teacherData = {
                  userId: res.data.id, // 后端返回的用户ID
                  teacherId: this.registerForm.username,
                  college: this.registerForm.college,
                  department: this.registerForm.department,
                  title: this.registerForm.title
                }
                
                // 添加导师信息
                return request.post('/teacher/add', teacherData)
              } else {
                this.$message.error(res.msg || '注册失败')
                this.loading = false
                return Promise.reject(new Error(res.msg || '注册失败'))
              }
            })
            .then(res => {
              if (res.code === 200) {
                this.$message.success('注册成功')
                this.$router.push('/login')
              } else {
                this.$message.error(res.msg || '导师信息添加失败')
              }
              this.loading = false
            })
            .catch(err => {
              console.error('注册失败:', err)
              this.$message.error(err.message || '注册失败，请稍后重试')
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    goToLogin() {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.register-container {
  height: 100%;
  width: 100%;
  background-color: #2d3a4b;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px 0;
}

.register-box {
  width: 500px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 5px;
  box-shadow: 0 0 10px #ccc;
}

.title {
  text-align: center;
  margin-bottom: 30px;
}

.title h2 {
  font-weight: bold;
  color: #409EFF;
}

.register-form {
  margin-top: 20px;
}

.register-button {
  width: 48%;
}

.back-button {
  width: 48%;
  float: right;
}
</style> 